package com.CST.util;

import com.CST.bo.zx.ProjectDetailBO;
import com.CST.bo.zx.ProjectSelectBO;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : 伍文毅
 * @create 2023/3/18 15:21
 */
public class ProjectTimeUtil {
    public static Map<String, Integer> operateTime(LocalDate startTime, LocalDate endTime) {
        int start = 0, end = 0;
        if (startTime.equals(endTime)) {
            int monthValue = startTime.getMonthValue();
            if (monthValue < 10) {
                start = Integer.parseInt(startTime.getYear() + "0" + monthValue);
                end = start;
//                if (monthValue == 9) {
//                    end = Integer.parseInt(startTime.getYear() + "" + (monthValue + 1));
//                } else {
//                    end = Integer.parseInt(startTime.getYear() + "0" + (monthValue + 1));
//                }
            } else {
                start = Integer.parseInt(startTime.getYear() + "" + monthValue);
                end = start;
//                if (monthValue == 12){
//                    end =  Integer.parseInt((startTime.getYear()+ 1) +"01");
//                }else {
//                    end =  Integer.parseInt(startTime.getYear() +""+ (monthValue + 1));
//                }
            }
        } else {
            int monthValue = startTime.getMonthValue();
            int value = endTime.getMonthValue();
            if (monthValue < 10) {
                start = Integer.parseInt(startTime.getYear() + "0" + monthValue);
                end = start;
                if (value >= 10) {
                    end = Integer.parseInt(endTime.getYear() + "" + value);
                } else {
                    end = Integer.parseInt(endTime.getYear() + "0" + value);
                }
            } else {
                start = Integer.parseInt(startTime.getYear() + "" + monthValue);
                if (value >= 10) {
                    end = Integer.parseInt(endTime.getYear() + "" + value);
                } else {
                    end = Integer.parseInt(endTime.getYear() + "0" + value);
                }
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("startPeriod", start);
        map.put("endPeriod", end);
        return map;
    }

    public static ProjectDetailBO setProjectDetailBO(ProjectDetailBO projectDetailBO) {
        Map<String, Integer> map = ProjectTimeUtil.operateTime(projectDetailBO.getStartTime(), projectDetailBO.getEndTime());
        Integer startPeriod = map.get("startPeriod");
        Integer endPeriod = map.get("endPeriod");
        projectDetailBO.setStartDate(startPeriod);
        projectDetailBO.setEndDate(endPeriod);
//        if (projectDetailBO.getStartTime().equals(projectDetailBO.getEndTime())) {
//            LocalDate localDate = projectDetailBO.getEndTime().plusMonths(1);
//            projectDetailBO.setEndTime(localDate);
//        }
        projectDetailBO.setStartNextMonthTime(projectDetailBO.getStartTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        projectDetailBO.setEndNextMonthTime(projectDetailBO.getEndTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        return projectDetailBO;
    }

    public static ProjectSelectBO setProjectDetailBO(ProjectSelectBO ProjectSelectBO) {
        Map<String, Integer> map = ProjectTimeUtil.operateTime(ProjectSelectBO.getStartTime(), ProjectSelectBO.getEndTime());
        Integer startPeriod = map.get("startPeriod");
        Integer endPeriod = map.get("endPeriod");
        ProjectSelectBO.setStartDate(startPeriod);
        ProjectSelectBO.setEndDate(endPeriod);
        if (ProjectSelectBO.getStartTime().equals(ProjectSelectBO.getEndTime())) {
            LocalDate localDate = ProjectSelectBO.getEndTime().plusMonths(1);
            ProjectSelectBO.setEndTime(localDate);
        }
        return ProjectSelectBO;
    }
}
